package com.mapler.autoresponder.service;

import com.mapler.model.AdModel;
import com.mapler.service.*;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;

public class IMAPAutoResponder {

    INotifier iNotifier;
    EmailModel emailModel;
    private static Logger log = Logger.getLogger(IMAPAutoResponder.class);

    public IMAPAutoResponder(INotifier iNotifier, EmailModel emailModel) {
        this.iNotifier = iNotifier;
        this.emailModel = emailModel;
    }

    IMAPAutoResponder() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void _reply(Message message) {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", emailModel.getSmtphost());
            props.put("mail.smtp.port", emailModel.getSmtpport());
            if (log.isDebugEnabled()) {
                props.put("mail.debug", "true");
            }

            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailModel.getUsername(), emailModel.getPassword());
                }
            });
            //Session session = Session.getDefaultInstance(props);
            Message _message = new MimeMessage(session);
            _message.setFrom(new InternetAddress("shahinur.bd@gmail.com"));
            _message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("shahinur.bd@gmail.com"));
            _message.setSubject("Testing Subject");
            _message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");

            Transport.send(_message);

            System.out.println("Done");
            /*Properties properties = System.getProperties();
             properties.setProperty("mail.smtp.host", "smtp.gmail.com");
             properties.setProperty("mail.smtp.port", "465");
             MimeMessage reply = (MimeMessage) message.reply(false);
             reply.setFrom(message.getFrom()[0]);
             MimeMessage orig = (MimeMessage) message;
             StringBuffer buffer = new StringBuffer("....................Thanks\n\n");
             if (orig.isMimeType("text/plain")) {
             String content = (String) orig.getContent();
             StringReader contentReader = new StringReader(content);
             BufferedReader br = new BufferedReader(contentReader);
             String contentLine;
             while ((contentLine = br.readLine()) != null) {
             buffer.append("> ");
             buffer.append(contentLine);
             buffer.append("\r\n");
             }
             }
             // Set the content
             reply.setText(buffer.toString());
             // Send the message
             Transport.send(reply);*/

        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    public void manualReply(MimeMessage message, AdModel adModel) {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", emailModel.getSmtphost());
            props.put("mail.smtp.port", emailModel.getSmtpport());
            if (log.isDebugEnabled()) {
                props.put("mail.debug", "true");
            }

            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailModel.getUsername(), emailModel.getPassword());
                }
            });

            Message _message = new MimeMessage(session);
            _message.setFrom(message.getRecipients(Message.RecipientType.TO)[0]);
            _message.setRecipient(Message.RecipientType.TO, message.getFrom()[0]);
            _message.setSubject("Re: " + message.getSubject());
            _message.setText("Thanks...");

            Transport.send(_message);
            System.out.println("Done");
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    public void reply(MimeMessage message) {
        try {
            MimeMessage reply = (MimeMessage) message.reply(false);
            reply.setFrom(message.getFrom()[0]);
            MimeMessage orig = (MimeMessage) message;
            StringBuilder buffer = new StringBuilder("....................Thanks\n\n");

            if (message.getContent() instanceof Multipart) {
                Multipart mp = (Multipart) message.getContent();
                int count = mp.getCount();
                for (int j = 0; j < count; j++) {
                    String contentLine = mp.getBodyPart(j).getContent().toString();
                    buffer.append("> ");
                    buffer.append(contentLine);
                    buffer.append("\r\n");
                }
            } else {
                String content = (String) orig.getContent();
                StringReader contentReader = new StringReader(content);
                BufferedReader br = new BufferedReader(contentReader);
                String contentLine;
                while ((contentLine = br.readLine()) != null) {
                    buffer.append("> ");
                    buffer.append(contentLine);
                    buffer.append("\r\n");
                }
            }
            // Set the content
            reply.setText(buffer.toString());
            Transport.send(reply);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    public INotifier getiNotifier() {
        return iNotifier;
    }

    public void setiNotifier(INotifier iNotifier) {
        this.iNotifier = iNotifier;
    }

    public EmailModel getEmailModel() {
        return emailModel;
    }

    public void setEmailModel(EmailModel emailModel) {
        this.emailModel = emailModel;
    }
}
